﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Services
{
	using System.Collections.Generic;
	using System.Threading;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain.Services;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Enums;
	using Falcon.Forums.Domain.Repositories;
	using Falcon.Forums.Domain.Services;
	using Falcon.Forums.Email;

	public class ForumTopicSubscriptionService : IForumTopicSubscriptionService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ForumTopicSubscriptionService"/> class.
		/// </summary>
		/// <param name="emailService">The email service.</param>
		/// <param name="forumTopicSubscriptionRepository">The forum topic subscription repository.</param>
		public ForumTopicSubscriptionService(
			IEmailService emailService, 
			IForumTopicSubscriptionRepository forumTopicSubscriptionRepository)
		{
			this.EmailService = emailService;
			this.ForumTopicSubscriptionRepository = forumTopicSubscriptionRepository;
		}

		/// <summary>
		/// Gets or sets the email service.
		/// </summary>
		/// <value>The email service.</value>
		public IEmailService EmailService
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets or sets the forum topic subscription repository.
		/// </summary>
		/// <value>The forum topic subscription repository.</value>
		public IForumTopicSubscriptionRepository ForumTopicSubscriptionRepository
		{
			get;
			private set;
		}

		public ForumTopicSubscription GetById(int id)
		{
			return this.ForumTopicSubscriptionRepository.GetById(id);
		}

		public ForumTopicSubscription GetBy(ForumTopicSubscriptionSpecification specification)
		{
			return this.ForumTopicSubscriptionRepository.GetBy(specification);
		}

		public IList<ForumTopicSubscription> GetAll(ForumTopicSubscriptionSpecification specification)
		{
			return this.ForumTopicSubscriptionRepository.GetAll(specification);
		}

		public IPagedList<ForumTopicSubscription, ForumTopicSubscriptionSpecification> GetPaged(ForumTopicSubscriptionSpecification specification)
		{
			return this.ForumTopicSubscriptionRepository.GetPaged(specification);
		}

		public ForumTopicSubscription Create()
		{
			return new ForumTopicSubscription();
		}

		public void Insert(ForumTopicSubscription subscription)
		{
			this.ForumTopicSubscriptionRepository.Insert(subscription);
		}

		public void Update(ForumTopicSubscription subscription)
		{
			this.ForumTopicSubscriptionRepository.Update(subscription);
		}

		public void Delete(ForumTopicSubscription subscription)
		{
			this.ForumTopicSubscriptionRepository.Delete(subscription);
		}

		/// <summary>
		/// Notifies the subscribers.
		/// </usmmary>
		/// <param name="specification">The specification.</param>
		public void NotifySubscribers(ForumTopicSubscriptionSpecification specification)
		{
			ThreadPool.QueueUserWorkItem(state => Notify(specification));
		}

		/// <summary>
		/// Notifies the subscribers that wish to me emailed using the specified specification.
		/// </summary>
		/// <param name="specification">The specification.</param>
		private void Notify(ForumTopicSubscriptionSpecification specification)
		{
			specification.Notification = ForumTopicSubscriptionNotification.SubscribeEmail;

			var subscriptions = this.ForumTopicSubscriptionRepository.GetAll(specification);

			foreach (var subscription in subscriptions)
			{
				if (subscription.UserId == subscription.Topic.LastPost.UserId)
				{
					continue;
				}

				this.EmailService.Send(new ForumTopicSubscriptionEmail
				{
					To = subscription.User.EmailAddress,
					Name = subscription.User.Name,
					Topic = subscription.Topic
				});
			}
		}
	}
}